package com.kuyun.base.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.kuyun.base.base.Page;
import com.kuyun.base.mapper.BaseRoleMapper;
import com.kuyun.base.mapper.BaseUserMapper;
import com.kuyun.base.model.BaseRole;
import com.kuyun.base.model.HttpResult;
import com.kuyun.base.model.ZtreeBean;
import com.kuyun.base.service.BaseRoleService;
import com.kuyun.base.util.Constants;
import com.kuyun.base.util.IDUtil;

/**
 * 角色Service实现类
 * @author 史梁辰
 * @date 2016-6-19下午9:49:27
 */
@Service("baseRoleService")
public class BaseRoleServiceImpl implements BaseRoleService {
	/**
	 * 角色数据接口对象
	 */
	@Resource
	private BaseRoleMapper roleMapper;
	@Resource
	private BaseUserMapper userMapper;

	/**
	 * 新增或修改角色 根据id判断
	 * @author 史梁辰
	 * @date 2016-6-19下午9:55:09
	 * @param role
	 * @return
	 * @throws Exception
	 */
	public boolean addOrUpdate(BaseRole role) throws Exception {
		int resultNum = 0;
		if (StringUtils.isEmpty(role.getId())) {// 判断id是否为空 为空则为新增 否则为修改
			role.setId(IDUtil.getId());
			role.setCreateTime(new Date());
			role.setCreateUserId("6bac7292b9354b25a6e8d0edbf0ee1c3");
			resultNum = roleMapper.insertRole(role);
		} else {
			role.setUpdateTime(new Date());
			role.setUpdateUserId("6bac7292b9354b25a6e8d0edbf0ee1c3");
			resultNum = roleMapper.updateRole(role);
		}
		if (resultNum > 0) {// 判断返回的受影响行数 如果大于0则有效 否则无效
			return true;
		} else {
			return false;
		}
	}
	/**
	 * 删除角色
	 * @author 史梁辰
	 * @date 2016-6-19下午10:35:25
	 * @param role
	 * @return
	 * @throws Exception
	 */
	public HttpResult deleteRole(String id) throws Exception {
		int resultNum = roleMapper.deleteRole(id);
		int resultNum1 = roleMapper.deleteUser_Role_ByRoleId(id);
		HttpResult result = new HttpResult();
		if ((resultNum > 0 && resultNum1 > 0) || resultNum > 0) {// 判断返回的受影响行数
																	// 如果大于0则有效
																	// 否则无效
			result.setResultCode(Constants.RESULT_SUCCESS);
			result.setDesc("删除角色成功");
		} else {
			result.setResultCode(Constants.RESULT_FINAL);
			result.setDesc("删除角色失败");
		}
		return result;
	}
	/**
	 * 查询角色数据 根据page对象判断查询全部还是分页查询
	 * @author 史梁辰
	 * @date 2016-6-19下午10:35:48
	 * @param page
	 * @param role
	 * @return
	 * @throws Exception
	 */
	public List<BaseRole> findRole(Page<BaseRole> page, BaseRole role) throws Exception {
		page.setPageNumber((page.getPageNumber() - 1) * page.getPageSize());
		return roleMapper.findRoles(page, role);
	}
	/**
	 * 根据id获取角色
	 * @author 史梁辰
	 * @date 2016-6-19下午10:37:55
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public BaseRole getRoleById(String id) throws Exception {
		return roleMapper.getRoleById(id);
	}
	public int getRoleCount(BaseRole role) {
		return roleMapper.getRoleCount(role);
	}
	public int checkRoleName(String name, String id) {
		return roleMapper.checkRoleName(name, id);
	}
	public List<ZtreeBean> getRoleTree(String id) {
		return roleMapper.getRoleTree(id);
	}
	public int saveUserRole(String ids, String id) throws Exception {
		userMapper.deleteUser_Role_ByUserId(id);// 先把该用户的角色关系全部删掉然后再添加新的
		String[] roleIds = ids.split(",");
		List<Map<String, String>> params = new ArrayList<Map<String, String>>();
		for (int i = 0; i < roleIds.length; i++) {
			Map<String, String> p = new HashMap<String, String>();
			p.put("id", IDUtil.getId());
			p.put("userId", id);
			p.put("roleId", roleIds[i]);
			params.add(p);
		}
		return roleMapper.saveUserRole(params);
	}
	public List<ZtreeBean> getPriTree(String id) {
		// TODO Auto-generated method stub
		return roleMapper.getPriTree(id);
	}
	public int saveRolePri(String ids, String id) {
		roleMapper.deletePriByRoleId(id);
		String[] priIds = ids.split(",");
		List<Map<String, String>> params = new ArrayList<Map<String, String>>();
		for (int i = 0; i < priIds.length; i++) {
			Map<String, String> p = new HashMap<String, String>();
			p.put("id", IDUtil.getId());
			p.put("roleId", id);
			p.put("priId", priIds[i]);
			params.add(p);
		}
		return roleMapper.saveRolePri(params);
	}
}
